package logic

import (
	"context"
	"rikao8/houserpc/models/mysql"

	"rikao8/houserpc/houserpc"
	"rikao8/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListLogic {
	return &ListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListLogic) List(in *houserpc.ListRequest) (*houserpc.ListResponse, error) {
	// todo: add your logic here and delete this line

	var house []mysql.House
	page := in.Page
	pageSize := in.PageSize
	if page <= 0 {
		page = 1
	}
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}
	offset := (page - 1) * pageSize
	mysql.DB.Debug().Offset(offset).Limit(in.PageSize).Find(&house)

	var slice []*houserpc.List
	for _, v := range house {
		list := houserpc.List{
			Id:      int32(v.ID),
			Name:    v.Name,
			Address: v.Address,
			Area:    int32(v.Area),
			Price:   float32(v.Price),
		}
		slice = append(slice, &list)
	}
	return &houserpc.ListResponse{List: slice}, nil
}
